home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / VD08SMP.ZIP / usr / samples / textview / textview.m < prev    next >
Encoding:
Text File  |  1996-02-13  |  2.4 KB  |  83 lines

  1. #include <pm/pm.h>
  2. #include <io.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5.  
  6. main(int argc,char *argv[])
  7. {
  8.   StdApp      *application;
  9.   StdWindow   *window;
  10.   Window      *mle;
  11.   FILE        *inputFile;
  12.   struct stat  statbuffer;
  13.   char        *contents;
  14.   char        *title;
  15.  
  16.   /*
  17.    * check for command line arguments and check given file (struct stat)
  18.    */
  19.   if (argc != 2) /* check for command line arguments, must be exactly one */
  20.     exit (-1);
  21.  
  22.   if (stat (argv[1],&statbuffer) < 0) /* check file */
  23.     exit (-1);
  24.  
  25.   /*
  26.    * open file and read contents to buffer
  27.    */
  28.   inputFile = fopen (argv[1],"r"); /* open text file read-only */
  29.  
  30.   contents = (char *) malloc (statbuffer.st_size + 1); /* allocate buffer */
  31.   fread (contents,statbuffer.st_size,1,inputFile); /* read contents of file */
  32.  
  33.   /*
  34.    * create app instance and window, create MLE for text display
  35.    */
  36.   application = [[StdApp alloc] init]; /* initialize application object */
  37.   window = [[MainWindow alloc] initWithId: 1000
  38.                                 andFlags: (FCF_SIZEBORDER | FCF_TITLEBAR |
  39.                        FCF_SYSMENU | FCF_MINMAX |
  40.                        FCF_SHELLPOSITION | FCF_TASKLIST)];
  41.   /* create main window */
  42.   
  43.   [window createObjects]; /* create child windows of main window */
  44.  
  45.   mle = [[MultiLineEntryField alloc] initWithId: 1001 
  46.                                        andFlags: (WS_VISIBLE | MLS_READONLY |
  47.                           MLS_HSCROLL | MLS_VSCROLL)
  48.                                              in: window];
  49.   [window insertChild: mle]; /* insert MLE into window */
  50.  
  51.   /*
  52.    * calculate title of window and set it
  53.    */
  54.   title = (char *) malloc (11 + /* this is the length of "Textview: " + NULL */
  55.                strlen (argv[1])); /* allocate buffer for title */
  56.   sprintf (title,"Textview: %s",argv[1]); /* fill title buffer */
  57.  
  58.   [window setTitle: title]; /* set window title */
  59.  
  60.   free (title); /* free title buffer */
  61.  
  62.   /*
  63.    * show window, set MLE size and  display contents of file
  64.    */
  65.   [window makeKeyAndOrderFront: nil]; /* show window */
  66.   [mle setSize: 0:0:[window width]:[window height]]; /* set MLE size */
  67.   [mle setText: contents]; /* display contents of file */
  68.  
  69.   /*
  70.    * run application
  71.    */
  72.   [application run];
  73.  
  74.   /*
  75.    * free all resources
  76.    */
  77.   free (contents); /* free contents buffer */
  78.   fclose (inputFile); /* close file */
  79.  
  80.   [application free]; /* free application */
  81.   [window free]; /* free window */
  82. }
  83.